package com.ych.modules.cms.authorInfc.menu.controller;

import com.ych.modules.cms.authorInfc.menu.service.IMenuService;
import com.ych.modules.base.dbEntity.TSysMenuEntity;
import com.ych.utils.TLMap;
import com.ych.utils.menu.MenuTree;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/21
 * Time: 14:45
 * Description:
 */
@Api(tags = {"CMS--菜单管理"}, description = "CMS--左边导航菜单")
@RestController
@RequestMapping("/cms/auth/menu")
public class CmsMenuController {
    @Autowired
    private IMenuService iMenuService;

    @ApiOperation(value = "导航菜单", position = 1)
    @GetMapping("/nav")
    public R<List<TSysMenuEntity>> nav() {
        try {
            List<TSysMenuEntity> menuList = iMenuService.menuByUserId(TLMap.getUserId());
            if (CollectionUtils.isEmpty(menuList)) {
                throw new RuntimeException("当前用户没有权限,请联系管理员先配置权限再登录");
            }
            return new R(0, "成功", menuList);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @ApiOperation(value = "根据userId查询当前用户权限", notes = "权限标签获取", position = 1)
    @PostMapping("/permsByUserId")
    public R<Set<String>> perms(@RequestBody String userId) {
        try {
            Set<String> stringSet = iMenuService.perms(userId);
            return new R(0, "成功", stringSet);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "根据userId查询当前用户菜单", notes = "菜单获取", position = 1)
    @PostMapping("/menuByUserId")
    public R<List<TSysMenuEntity>> menuByUserId(@RequestBody String userId) {
        try {
            List<TSysMenuEntity> list = iMenuService.menuByUserId(userId);
            return new R(0, "查询成功", list);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:menu")
    @ApiOperation(value = "所有菜单列表", position = 2)
    @PostMapping("/list")
    public R<MenuTree> list() {
        try {
            MenuTree menuTree = iMenuService.list();
            return new R(0, "查询成功", menuTree);
        }catch (Exception e){
            return new R(1,e.getMessage(),null);
        }
    }

    @RequiresPermissions("cms:auth:menu")
    @ApiOperation(value = "选择菜单(添加、修改菜单)", position = 3)
    @GetMapping("/select")
    public R<List<TSysMenuEntity>> select() {
        try {
            List<TSysMenuEntity> tSysMenuEntities = iMenuService.select();
            return new R(0, "成功", tSysMenuEntities);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "菜单信息", position = 4)
    @PostMapping("/info")
    public R<TSysMenuEntity> info(@RequestBody String menuIdStr) {
        try {
            TSysMenuEntity tSysMenuEntity = iMenuService.info(menuIdStr);
            return new R(0, "查询成功", tSysMenuEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:menu")
    @ApiOperation(value = "保存菜单", position = 5)
    @PostMapping("/save")
    public R<String> save(@RequestBody TSysMenuEntity menu) {
        try {
            iMenuService.save(menu);
            return new R(0, "保存成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:menu")
    @ApiOperation(value = "修改菜单", position = 6)
    @PostMapping("/update")
    public R<String> update(@RequestBody TSysMenuEntity menu) {
        try {
            iMenuService.update(menu);
            return new R(0, "修改成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }

    }

    @RequiresPermissions("cms:auth:menu")
    @ApiOperation(value = "删除菜单", position = 7)
    @PostMapping("/delete")
    public R<String> delete(@RequestBody String menuIdStr) {
        try {
            iMenuService.delete(menuIdStr);
            return new R(0, "删除成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
